package pri.zey.custom.tools;/**
 * Created by Administrator on 2016/3/16.
 */

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * 基础信息工具类
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 *
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 * isNetWorkConnected()---->检测网络是否可用
 * isExitsSdcard()---->检测Sdcard是否存在
 * getPageName()---->获取当前包名
 * getDeviceWidth()---->获取设备屏幕宽度
 * getDeviceHeight()---->获取设备屏幕高度
 * getFilesDir()---->获取当前程序路径
 * getPackageResourcePath()---->获取该程序的安装包路径
 * getDatabasePath()---->获取程序默认数据库路径
 * getTopActivityClassName()---->获取当前活动Activity Name
 * hideSoftInput()---->隐藏软键盘
 * isSoftInputShowed()---->判断软键盘状态
 * showSoftInput()---->显示软键盘
 */
public class TbaseInfo {

    private Context mContext;

    public TbaseInfo(Context mContext){
        this.mContext = mContext;
    }

    /**
     * 检测网络是否可用
     * @return
     */
    public boolean isNetWorkConnected() {
        if (mContext != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
            }
        }

        return false;
    }

    /**
     * 获取设备屏幕宽度
     * @return
     */
    @SuppressWarnings("deprecation")
    public int getDeviceWidth() {
        WindowManager manager = (WindowManager) mContext
                .getSystemService(mContext.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getWidth();
    }

    /**
     * 获取设备屏幕高度
     * @return
     */
    @SuppressWarnings("deprecation")
    public int getDeviceHeight() {
        WindowManager manager = (WindowManager) mContext
                .getSystemService(mContext.WINDOW_SERVICE);
        return manager.getDefaultDisplay().getHeight();
    }

    /**
     * 检测Sdcard是否存在
     *
     * @return
     */
    public static boolean isExitsSdcard() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }


    /**
     * 获取当前包名
     * @return
     */
    public String getPageName(){
        return mContext.getPackageName();
    }

    /**
     * 获取当前程序路径
     * @return
     */
    public String getFilesDir(){
        return mContext.getFilesDir().getAbsolutePath();
    }

    /**
     * 获取该程序的安装包路径
     * @return
     */
    public String getPackageResourcePath(){
        return mContext.getPackageResourcePath();
    }

    /**
     * 获取程序默认数据库路径
     * @return
     */
    public String getDatabasePath(){
        return mContext.getDatabasePath(getPageName()).getAbsolutePath();
    }

    /**
     * 获取当前活动ActivityName
     * @return
     */
    public String getTopActivityClassName(){
        ActivityManager mActivityManager = (ActivityManager) mContext.getSystemService(mContext.ACTIVITY_SERVICE);
        ComponentName mComponentName = mActivityManager.getRunningTasks(1).get(0).topActivity;
        return mComponentName.getClassName();
    }

    /**
     * 隐藏软键盘
     *
     * @param context
     */
    public static void hideSoftInput(Activity context) {
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }

    /**
     * 隐藏软键盘
     *
     * @param context
     */
    public static void hideSoftInput(Activity context, EditText edt) {
        try {
            ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(edt.getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断软键盘状态
     *
     * @param context
     */
    public static boolean isSoftInputShowed(Activity context) {
        int softInputMode = context.getWindow().getAttributes().softInputMode;
        return softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED;
    }

    /**
     * 显示软键盘
     *
     * @param context
     * @param view
     */
    public static void showSoftInput(Activity context, View view) {
        ((InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE)).showSoftInput(view, 0);
    }
}
